/**
* @Title: warning_ctrl.js
* @Description: TODO
* Copyright: Copyright (c) 2014 
 * Company:中国平安
*
* @Project HIMS-FWA
* @date 2014年10月27日 下午4:43:38
* @version V1.0
*/
;
(function(){
	'use strict';
	
	var app = angular.module('fwa.mainApp');
	app.controller('CaseCommonAddStaffCtrl', ['$scope','$http','$modal','BaseURL','Modal','$modalInstance',
	                                          'FwaTabsetService','CommunityService','$stateParams','$state','InsuranceService',
	                                function($scope,$http,$modal,baseUrl,Modal,$modalInstance,
	                                		fwaTabsetService,CommunityService,$stateParams,$state,InsuranceService){
		
		$scope.current={
				limit:20,
				currentPage:1,
				totalCount:0
		};
		
		InsuranceService.initMedicalOrgInfoList().success(function(response){
	            $scope.medicalOrgInfoList = response.medicalOrgList;
			});
		
		$scope.communityService = CommunityService;
		CommunityService.getBaseCode('{codeType:\'cityDep\'}');
		
		 $scope.queryParamter={
				 page:1,
				 pageSize:$scope.current.limit
		 };
		 
		 $scope.form={};
		
		 $scope.staffColumns = [
	                          {label:'姓名',map:'name',width:'20%'},
	                          {label:'性别',map:'gender',width:'10%'},
	                          {label:'身份证号',map:'idCard',width:'20%'},
	                          {label:'人员类别',map:'personType',width:'10%'},
	                          {label:'机构名称',map:'medicalName',width:'20%'},
	                          {label:'科室名称',map:'departName',width:'20%'}
	                          
	                         ];
	       $scope.staffTableData = [];
		
		
		/**
		 * 关闭modal事件
		 */
		$scope.onClose = function() {
			$modalInstance.close();
		}
		
		
		/**
		 * 弹出框确定事件
		 */
		$scope.onModalConfirm=function(){
			var array=$scope.checkRows;
			if(array.length>=0){
				
				processNameAndCode(array);
				$scope.callStaffFun(array);
				$scope.onClose();
				
			}else{
				Modal.alert('请选择涉及的医务人员');
				return;
			}
			
			
		}
		
		
		/**
		 * 替换name和code
		 */
		function processNameAndCode(items){
			
			$.each(items,function(index,item){
				item['code']=item.medicalCode;
				item['selfCode']=item.staffCode;
			});
			
		}
		
		
		    $scope.checkRows = [];

	        function findCheck(d) {
	            for(var i=0;i< $scope.checkRows .length;i++) {
	                var checkedData =  $scope.checkRows [i];
	                if(checkedData.staffCode == d.staffCode) {
	                    return i;
	                }
	            }
	            return -1;
	        }

	        $scope.$on('fwatable.checked', function(e,d){
	            if(findCheck(d) == -1) {
	                $scope.checkRows.push(d);
	            }
	        });

	        $scope.$on('fwatable.unchecked', function(e,d){
	            var index = findCheck(d);
	            if(index != -1) {
	                $scope.checkRows.splice(index,1);
	            }
	        });
		
		
		
		//分页监听
		$scope.$on('pageTool.skipPage',function(e,v){
			$scope.current.currentPage = v;
			$scope.queryParamter.page=v;
			$scope.onSubmit();
		});
		
		
		/**
		 * 获取查询条件
		 */
		function getParamter(){
			$scope.form.page=$scope.queryParamter.page;
			$scope.form.pageSize=$scope.queryParamter.pageSize;
			
			return $scope.form;
		}
		
		/**
		 * 查询事件
		 */
		$scope.onSubmit=function(){
			var obj=getParamter();
			   $http({
					method : 'post',
					cache : false,
					url : baseUrl + 'caseCommonDispatch/loadCaseCommonStaff.do',
					params :obj
				}).success(function(response, status, headers, config) {
					$scope.staffTableData=response.staffList;
					$scope.current.totalCount=response.staffCount;
				});
			
		}
		
		/**
		 * 重置函数
		 */
		$scope.onReset=function(){
			$scope.form={};
		}
		
		$scope.onSubmit();
       
	
	}]);
})();
